<?php

class Service_Ferry_Ticket_Code extends Contract_Suit
{
    public $params;
    public $code_img;

    public function __construct($params)
    {
        $this->set_params($params);
    }

    /**
     * @throws Exception
     */
    public function suit_check()
    {
        $is_open_multiple_ticket = DB::select()->from('ferry')->where('id','=',$this->params['product_id'])->execute()->get('is_open_multiple_ticket');
        $scan_code_is_open = DB::select()->from('sysconfig')->where('varname','=','cfg_scan_code_5_is_open')->execute()->get('value');
        if($is_open_multiple_ticket==1 || $scan_code_is_open==1){
            $this->set_code_img();
        }else{
            $sku = DB::select()->from('ferry_ticket_sku')->where('ticket_id','=',$this->params['suit_id'])->execute()->valid();
            if($sku){
                $this->set_code_img();
            }else{
                throw new Exception('该门票不支持扫码下单');
            }
        }
    }

    public function set_code_img()
    {
        $appid  = Model_Api_V2_System::get_fenxiao_para('cfg_wxxcx_appid');
        $secret = Model_Api_V2_System::get_fenxiao_para('cfg_wxxcx_appsecret');
        $is_xcx = DB::select()->from('sysconfig')->where('varname','=','cfg_scan_code_5_xcx')->execute()->get('value');
        $product_url = '/subPackages/ferrys/detail';
        $xcx_img_path = '';
        if ($appid && $secret && $is_xcx==1) {
            // 获取分销配置信息
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $access_token = Model_Api_V2_Pub::makeRequest($url);
            $access_token = json_decode($access_token['result'], true);

            $codeRes = Model_Api_V2_Pub::getWxcode($product_url, $this->params['product_id'], '', $access_token, Model_Ferry::$typeid);
            if ($codeRes['status']) {
                // 保存路径及文件名
                $path = '/uploads/uniapp/productCode/wechat/'.date('Y').'/';
                $filename = md5($product_url.$this->params['product_id']);
                // 创建图片并返回路径
                $xcx_img_path = Model_Api_V2_Pub::base64_image_content($codeRes['data'], $path, $filename);
            }
        }
        $h5_img_path = $GLOBALS['cfg_basehost'].'/h5/#'.$product_url.'?id='.$this->params['product_id'];

        $this->code_img['web']  = $h5_img_path;
        $this->code_img['xcx'] = $xcx_img_path;
    }

}